package hzn.controller;

import hzn.beans.Book;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/book")
public class SolrCtrl {
    @Autowired
    HttpSolrClient client;
    @RequestMapping("/addBook")
    public void add() throws Exception {
        Book b1 = new Book(1, "吴子兵法", "战国初期吴起所著", "吴起", 12);
        Book b2 = new Book(2, "三国演义", "天下事，合久必分。。。", "罗贯中", 10);
        Book b3 = new Book(3, "孙子兵法", "兵者，诡道也，国之利器，生死之地，存亡之道，不可不查也", "孙武", 32);
        Book b4 = new Book(4, "西游记", "一只猴子的故事。。。", "吴承恩", 9);
        List<Book> list = Arrays.asList(b1, b2, b3, b4);
        for (Book b : list) {
            SolrInputDocument d = new SolrInputDocument();
            d.addField("id", b.getId());
            d.addField("bookname", b.getName());
            d.addField("author", b.getAuthor());
            d.addField("bookintroduce", b.getIntroduce());
            d.addField("bookprice", b.getPrice());
            client.add(d);
            client.commit();
        }
    }

    @RequestMapping("/searchBook")
    public Map<String, Object> search(String kw, Integer pageNo, Integer pageSize) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String param = null;
        if (kw == null || kw.equals("")) {
            param = "*:*";
        } else {
            param = "bookname:" + kw + " OR author:" + kw + " OR bookintroduce:" + kw + " OR bookprice:" + kw;
        }
        SolrQuery q = new SolrQuery();
        q.setQuery(param);
        q.setSort("bookprice", SolrQuery.ORDER.asc);
        //设置查询从哪一条开始
        q.setStart(pageSize * (pageNo - 1));
        q.setRows(pageSize);
        //开始查询
        QueryResponse resp=null;
        try {
            resp = client.query(q);
        } catch (Exception e) {
            return map;
        }
        //解析查询结果
        SolrDocumentList rs = resp.getResults();
        Long total = rs.getNumFound();
        System.out.println("共查到" + total);
        List<Map<String, String>> list = new ArrayList<>();
        for (SolrDocument sd : rs) {
            Map<String, String> vo = new HashMap<>();//用来放一本书
            String tmp = (String) sd.getFirstValue("id");
            vo.put("id", tmp);
            tmp = (String) sd.getFirstValue("bookname");
            vo.put("bookname", tmp);
            tmp = (String) sd.getFirstValue("author");
            vo.put("author", tmp);
            tmp = (String) sd.getFirstValue("bookintroduce");
            vo.put("bookintroduce", tmp);
            tmp = sd.getFirstValue("bookprice") + "";
            vo.put("bookprice", tmp);
            list.add(vo);
        }

        map.put("list", list);
        map.put("total", total);
        map.put("pagesTotal", (total % pageSize == 0) ? (total / pageSize) : (total / pageSize + 1));
        map.put("pageNum", pageNo);
        map.put("pageSize", pageSize);
        return map;
    }
}
